package com.ley.springboot.commons.utils;


import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * PathMatchingResourcePatternResolver utility class
 *
 * @author 2036185346@qq.com
 * @see PathMatchingResourcePatternResolver
 * @see Resource
 **/
@Slf4j
public class PathMatchingResourcePatternResolverUtils {

    /**
     * path match thread local
     **/
    private static final ThreadLocal<PathMatchingResourcePatternResolver>
            pathMatchThreadLocal = new ThreadLocal<PathMatchingResourcePatternResolver>() {
        @Override
        protected PathMatchingResourcePatternResolver initialValue() {
            return new PathMatchingResourcePatternResolver();
        }
    };


    /**
     * default location pattern delimiter
     **/
    public static final String DEFAULT_LOCATION_PATTERN_DELIMITER = ",";


    /**
     * get resources
     *
     * @param locationPattern location pattern and this location pattern support Ant style,such as *.properties.<br/>
     * @see PathMatchingResourcePatternResolver#getResources(String)
     **/
    public static Resource[] getResources(String locationPattern) {
        PathMatchingResourcePatternResolver pathMatchingResourcePatternResolver = pathMatchThreadLocal.get();
        Assert.notNull(pathMatchingResourcePatternResolver, "pathMatchingResourcePatternResolver can't null.");
        Resource[] resources = null;
        try {
            resources = pathMatchingResourcePatternResolver.getResources(locationPattern);
        } catch (IOException e) {
            log.info(e.getMessage());
        }
        return resources;
    }


    /**
     * get resources
     *
     * @param locationPatterns location pattern and this location pattern support Ant style
     * @param delimiter        delimiter,default delimiter is {@link PathMatchingResourcePatternResolverUtils#DEFAULT_LOCATION_PATTERN_DELIMITER}
     * @see PathMatchingResourcePatternResolver#getResources(String)
     **/
    public static Resource[] getResources(String locationPatterns, String delimiter) {
        if (!StringUtils.hasText(delimiter)) {
            delimiter = DEFAULT_LOCATION_PATTERN_DELIMITER;
        }
        String[] locationPatternArray = StringUtils.split(locationPatterns, delimiter);
        List<Resource> resourcesList = new ArrayList<>();
        for (String locationPattern : locationPatternArray) {
            Resource[] resources = getResources(locationPattern);
            resourcesList.addAll(Arrays.asList(resources));
        }
        //list to array and use generic type
        Resource[] resourcesAll = resourcesList.toArray(new Resource[resourcesList.size()]);
        return resourcesAll;
    }

    /**
     * get resource
     *
     * @param location resource location
     * @see PathMatchingResourcePatternResolver#getResource(String)
     **/
    public static Resource getResource(String location) {
        PathMatchingResourcePatternResolver pathMatchingResourcePatternResolver = pathMatchThreadLocal.get();
        log.debug("location: {}", location);
        return pathMatchingResourcePatternResolver.getResource(location);
    }
}
